Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
React State Hook initialised to [] still shows type as undefined

Code:

const [input, setInput] = useState('');
const [studentList, setStudentList] = useState([]);

useEffect(() => {
  console.log(studentList)
  console.log(studentList.type)
}, [studentList]);

return (
  <div id="add-students-input-div">
    <input
      type="text"
      id='add-students-input'
      value={input}
      placeholder='Enter a student to the database'
      onChange={(event) => {
        setInput(event.target.value)
      }}
    />
    <div id="add-students-button" onClick={() => {
      setStudentList([document.getElementById('add-students-input').value, ...studentList])
      setInput('')
     }}>
      <p>Add</p>
    </div>
  </div>
)

Problem: The print statement for studentList is returning the array but the print statement for studentList.type is undefined at all times, even after elements are added to the array.

How can I ensure that studentList.type returns Array.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

studentList in your code will ever be an array, empty when you initialize the state. If you want to check if there is something in the array you have to use studentList.length

about 4 years ago · Santiago Trujillo Denunciar

0

As mentioned in the comments, arrays do not have a type property.

Your studentList state value is always an array; there is no need to check its type.

One thing you do appear to be doing incorrectly is updating studentList when you click your button (<div>). In short, you really shouldn't need to use DOM methods in React.

To update your array with the value from your input state, use this...

const handleClick = () => {
  setStudentList((prev) => [input, ...prev]);
  setInput("");
};

and

<div id="add-students-button" onClick={handleClick}>
  <p>Add</p>
</div>

See useState - Functional updates for information on how I'm using setStudentList()

about 4 years ago · Santiago Trujillo Denunciar

0

Altough previous contributors solved your problem by eliminating it in other place, I would like to answer this:

How can I ensure that studentList.type returns Array

If you want to make sure your variable is an array, you may use isArray() static method of Array:

Array.isArray(studentList) // returns true if array or false if not
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda